home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / StandardFileIcons / ShowIcon7.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-23  |  7.3 KB  |  255 lines  |  [TEXT/MPS ]

  1. #include <Icons.h>
  2. #include <OSUtils.h>
  3. #include <Resources.h>
  4. #include <Memory.h>
  5. /*
  6.     ShowIcon7.c
  7.     
  8.     ShowINIT compatible routine that shows 'ICN#' and 'iclx' flavor icons.
  9.     For use by all INITs in System 7 and beyond.
  10.     
  11.     This code is based on Patrick C. Beard's ShowIconFamily
  12.     by Patrick C. Beard, which in turn was derived from the
  13.     original ShowInit by Paul Mercer, Darin Adler,
  14.     Paul Snively, and Steve Capps.
  15.     
  16.     Modified by James W. Walker for compatibility with IconWrap 1.2,
  17.     for use as a separate code resource, and to use the new System 7
  18.     icon-drawing routines.  This code is in the public domain.
  19.  
  20.     [Modified to remove in-line assembly code by Brian Bechtel, blob@apple.com]
  21.     
  22.     Addresses for James W. Walker: 76367,2271@compuserve.com or
  23.         walkerj@math.scarolina.edu
  24.     
  25.     Instructions for use:
  26.     
  27.         • Create a family of icons with ResEdit 2.1 or later.  This will
  28.             include 'ICN#', 'icl4', & 'icl8' icons, all with the
  29.             same resource ID.
  30.         
  31.         To use within a larger INIT project:
  32.             • #define SEPARATE_CODE 0  below.
  33.             • Call ShowIcon7( id, adv ) with the resource id of
  34.                 the family that you used.  The Boolean parameter adv
  35.                 indicates whether you want the next use of ShowIcon7
  36.                 to advance to a new position.  Normally you pass TRUE,
  37.                 but you can get an animated-icon effect by passing FALSE
  38.                 all except the last time you call ShowIcon7.
  39.         
  40.         To use as a separate code resource:
  41.             • #define SEPARATE_CODE 1  below.
  42.             • Set the project type to code resource, set the code type
  43.                 and resource ID (I use type 'Code', ID -4048 for cdevs),
  44.                 compile and merge into your INIT or cdev file.
  45.                 Set the resource type to Locked, so you won't have
  46.                 to call HLock when you use it.
  47.             • In your main INIT code, call ShowIcon7 like so:
  48.                 pascal void (*ShowIcon7)( short resid, Boolean adv );
  49.                 Handle  show_init;
  50.                 
  51.                 show_init = GetResource( 'Code', -4048 );
  52.                 ShowIcon7 = (pascal void (*)(short, Boolean))
  53.                             StripAddress( *show_init );
  54.                 ShowIcon7( iconID, advance );
  55.  */
  56.  
  57. #define SEPARATE_CODE    0
  58.  
  59.  
  60. #if SEPARATE_CODE
  61. #define        ShowIcon7        main    /* JWW */
  62. #endif
  63.  
  64. #ifndef nil
  65. #define nil ((void*)0L)
  66. #endif
  67.  
  68. #ifndef topLeft
  69.     #define topLeft(r) ((Point*)&r)[0])
  70. #endif
  71. #ifndef botRight
  72.     #define botRight(r) ((Point*)&r)[1])
  73. #endif
  74.  
  75. typedef struct myQDGlobals {
  76.     char privates[76];
  77.     long randSeed;
  78.     BitMap screenBits;
  79.     Cursor arrow;
  80.     Pattern dkGray;
  81.     Pattern ltGray;
  82.     Pattern gray;
  83.     Pattern black;
  84.     Pattern white;
  85.     GrafPtr thePort;
  86.     long    end;
  87. } myQDGlobals;
  88.  
  89.  
  90. /* prototypes */
  91. pascal void ShowIcon7(short iconId, Boolean advance);
  92. static void DrawBWIcon( short iconId, Rect *icon_rect, Boolean visible );
  93. static void Next_position( void );
  94. static void GetIconRect( register Rect* iconRect, Rect *screen_rect );
  95.  
  96. /* this is where it all happens. */
  97.  
  98. pascal void ShowIcon7( short iconId, Boolean advance )
  99. {
  100.     long    oldA5;
  101.     myQDGlobals qd;                /* our QD globals. */
  102.     SysEnvRec environment;        /* machine configuration. */
  103.     CGrafPort gp;                /* our grafport. */
  104.     Rect    icon_rect;
  105.     OSErr    err;
  106.     
  107.     /* get a value for A5, a structure that mirrors qd globals. */
  108.     oldA5 = SetA5((long)&qd.end);
  109.     InitGraf(&qd.thePort);
  110.     
  111.     /* find out what kind of machine this is. */
  112.     SysEnvirons(curSysEnvVers, &environment);
  113.  
  114.     GetIconRect( &icon_rect, &qd.screenBits.bounds );
  115.  
  116.     /*
  117.         The old IconWrap INIT patches CopyBits but does not know about
  118.         CopyMask or IconDispatch.  So in the color case, we draw the
  119.         black and white icon with an empty mask region.
  120.     */
  121.  
  122.     if ( (environment.systemVersion >= 0x0700) && environment.hasColorQD )
  123.     {
  124.         OpenCPort(&gp);
  125.         DrawBWIcon( iconId, &icon_rect, false );
  126.         err = PlotIconID( &icon_rect, atNone, ttNone, iconId );
  127.         CloseCPort(&gp);
  128.     }
  129.     else
  130.     {
  131.         OpenPort((GrafPtr)&gp);
  132.         DrawBWIcon( iconId, &icon_rect, true );
  133.         ClosePort((GrafPtr)&gp);
  134.     }
  135.     
  136.     if (advance)
  137.         Next_position(); /* JWW */
  138.  
  139.     SetA5(oldA5);
  140. }
  141.  
  142. /*
  143.     ShowInit's information is nestled at the tail end of CurApName.
  144.     It consists of a short which encodes the next horizontal offset,
  145.     and another short which is that value checksummed with the function below.
  146.  */
  147.  
  148. #define CurApName_LM    0x910
  149. #define ShowINITTable ((unsigned short*)(CurApName_LM + 32 - 4))
  150. #define CheckSumConst 0x1021        /* magic value to check-sum with. */
  151.  
  152. #define InitialXPosition 8            /* initial horizontal offset. */
  153. #define YOffset            40            /* constant from bottom to place the icon. */
  154. #define XOffset            40            /* amount to change it by. */
  155. #define ICON_WIDTH        32
  156. #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
  157.  
  158. /* CheckSum() computes the magic value to determine if ShowInit's have run already. */
  159.  
  160. static short CheckSum(register unsigned short x)
  161. {
  162. //    asm {
  163. //        rol.w    #1, x
  164. //    }
  165.     if (x & 0x8000)    /* high bit set */
  166.         return((x << 1) ^ CheckSumConst ^ 0x01);
  167.     else
  168.         return ((x << 1) ^ CheckSumConst);
  169. }
  170.  
  171. /*
  172.     GetIconRect() generates an appropriate rectangle to display the
  173.     next INIT's icon in.
  174.     It is also responsible for updating the horizontal
  175.     position in low memory.  This is a departure from
  176.     the original ShowInit code, which updates low
  177.     memory AFTER displaying the icon. -- changed by JWW
  178.     This code won't generate an icon position until it is certain that the icon can be loaded, so the
  179.     same behaviour occurs.
  180.     
  181.     This routine also generates a rectangle which is guaranteed to be onscreen.  It
  182.     does this by taking the horizontal offset modulo the screen width to generate
  183.     the horizontal position of the icon, and the offset divided by the screen
  184.     width to generate the proper row.
  185.  */
  186.  
  187. static void GetIconRect(register Rect* iconRect, Rect *screen_rect )
  188. {
  189.     register short screenWidth;
  190.     
  191.     screenWidth = screen_rect->right - screen_rect->left;
  192.     screenWidth -= screenWidth % XOffset;
  193.     /* if we are the first INIT to run we need to initialize the horizontal value. */
  194.     if (CheckSum(ShowINITTable[0]) != ShowINITTable[1])
  195.         ShowINITTable[0] = InitialXPosition;
  196.     
  197.     /* compute top left of icon's rect. */
  198.     iconRect->left = (ShowINITTable[0] % screenWidth);
  199.     iconRect->top = screen_rect->bottom -
  200.         YOffset * (1 + (ShowINITTable[0] / screenWidth));
  201.     iconRect->right = iconRect->left + 32;
  202.     iconRect->bottom = iconRect->top + 32;
  203.     
  204. }
  205.  
  206. /*
  207.     JWW: In Beard's original version, this was done at the end of
  208.     GetIconRect. That caused incorrect behavior when IconWrap 1.2 was
  209.     used to wrap icons.  Namely, if an INIT using that version of
  210.     ShowIconFamily was the first in a row, then the second icon in that
  211.     row would land on top of it.
  212. */
  213. static void Next_position( void )
  214. {
  215.     /* advance the position for the next icon. */
  216.     ShowINITTable[0] += XOffset;
  217.     
  218.     /* recompute the checksum. */
  219.     ShowINITTable[1] = CheckSum(ShowINITTable[0]);    
  220. }
  221.  
  222. /* DrawBWIcon() draws the 'ICN#' member of the icon family. */
  223.  
  224. void DrawBWIcon( short iconId, Rect *icon_rect, Boolean visible )
  225. {
  226.     Handle        icon;
  227.     BitMap        source, destination;
  228.     RgnHandle    empty_mask;
  229.     GrafPtr        port;
  230.     
  231.     icon = Get1Resource('ICN#', iconId);
  232.     if (!icon)
  233.         return;
  234.     HLock(icon);
  235.     
  236.     /* prepare the source and destination bitmaps. */
  237.     source.baseAddr = *icon + 128;                    /* mask address. */
  238.     source.rowBytes = 4;
  239.     SetRect(&source.bounds, 0, 0, 32, 32);
  240.     GetPort( &port );
  241.     destination = ((GrafPtr)port)->portBits;
  242.     empty_mask = visible? nil : NewRgn();
  243.     
  244.     /* transfer the mask. */
  245.     CopyBits(&source, &destination, &source.bounds, icon_rect,
  246.         srcBic, empty_mask);
  247.     
  248.     /* and the icon. */
  249.     source.baseAddr = *icon;    
  250.     CopyBits(&source, &destination, &source.bounds, icon_rect,
  251.         srcOr, empty_mask);
  252.     
  253.     if (empty_mask) DisposeRgn( empty_mask );
  254. }
  255.